<template>
  <div class="api-test-container">
    <h1>API 测试页面</h1>
    
    <el-card class="test-card">
      <template #header>
        <div class="card-header">
          <span>API 连接测试</span>
        </div>
      </template>
      
      <div class="test-actions">
        <el-button type="primary" @click="testDashboardSummary" :loading="loading">
          测试 Dashboard Summary API
        </el-button>
        
        <el-button type="primary" @click="testDeviceStatus" :loading="loading">
          测试 Device Status API
        </el-button>
        
        <el-button type="primary" @click="testProductionHistory" :loading="loading">
          测试 Production History API
        </el-button>
      </div>
      
      <div v-if="result" class="result-container">
        <h3>API 响应结果：</h3>
        <pre>{{ JSON.stringify(result, null, 2) }}</pre>
      </div>
      
      <div v-if="error" class="error-container">
        <h3>API 错误：</h3>
        <pre>{{ error }}</pre>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import api from '../services/api'

const loading = ref(false)
const result = ref(null)
const error = ref(null)

const clearResults = () => {
  result.value = null
  error.value = null
}

const testDashboardSummary = async () => {
  clearResults()
  loading.value = true
  
  try {
    const response = await api.getDashboardSummary()
    result.value = response.data
  } catch (err) {
    error.value = err.message || 'Failed to fetch dashboard summary'
    console.error('Error fetching dashboard summary:', err)
  } finally {
    loading.value = false
  }
}

const testDeviceStatus = async () => {
  clearResults()
  loading.value = true
  
  try {
    const response = await api.getDeviceStatus()
    result.value = response.data
  } catch (err) {
    error.value = err.message || 'Failed to fetch device status'
    console.error('Error fetching device status:', err)
  } finally {
    loading.value = false
  }
}

const testProductionHistory = async () => {
  clearResults()
  loading.value = true
  
  try {
    const response = await api.getProductionHistory('day')
    result.value = response.data
  } catch (err) {
    error.value = err.message || 'Failed to fetch production history'
    console.error('Error fetching production history:', err)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.api-test-container {
  padding: 20px;
  background-color: #f5f7fa;
  height: 100%;
  overflow-y: auto;
}

h1 {
  margin-bottom: 20px;
}

.test-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.test-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.result-container,
.error-container {
  margin-top: 20px;
  padding: 15px;
  border-radius: 4px;
}

.result-container {
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
}

.error-container {
  background-color: #fef0f0;
  border: 1px solid #fde2e2;
  color: #f56c6c;
}

pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  background-color: #f8f8f8;
  padding: 10px;
  border-radius: 4px;
  max-height: 400px;
  overflow-y: auto;
}
</style>
